<?php

namespace Gairal\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Image
 *
 * @ORM\Table(name="gairal_image")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Image {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;
    /**
     * @var type UploadedFile
     * @Assert\Image(
     *      minWidth=20,
     *      maxWidth=500,
     *      minHeight=20,
     *      maxHeight=500
     * )
     */
    private $file;
    private $tempFilename;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Image
     */
    public function setName($name) {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName() {
        return $this->name;
    }

    /**
     * Set file
     *
     * @param string $name
     * @return Image
     */
    public function setFile(UploadedFile $file) {
        $this->file = $file;
        
        if(null !== $this->name){
            $this->tempFilename = $this->name;
            $this->name = null;
        }

        return $this;
    }

    /**
     * Get file
     *
     * @return string 
     */
    public function getFile() {
        return $this->file;
    }
    
    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function preUpload(){
        if (null === $this->file) {
            return;
        }
        
        $this->name = $this->file->getClientOriginalName();
    }

    /**
     * @ORM\PostPersist
     * @ORM\PostUpdate
     */
    public function upload() {
        if (null === $this->file) {
            return;
        }
        
        if(null !== $this->tempFilename){
            $oldFile = $this->getUploadRootDir().'/'.$this->id.'_'.$this->tempFilename;
            if(file_exists($oldFile)){
                unlink($oldFile);
            }
        }
        $this->file->move(
            $this->getUploadRootDir(),
            $this->id.'_'.$this->name);
    }
    
    /**
     * @ORM\PreRemove
     */
    public function preRemoveUpload()
    {
        $this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'_'.$this->name;
    }
    
    /**
     * @ORM\PostRemove
     */
    public function removeUpload(){
        if(file_exists($this->tempFilename)){
            unlink($this->tempFilename);
        }
    }

    public function getUploadDir() {
        // On retourne le chemin relatif vers l'image pour un navigateur
        return 'img/uploads';
    }

    protected function getUploadRootDir() {
        // On retourne le chemin relatif vers l'image pour notre code PHP
        return __DIR__ . '/../../../../web/' . $this->getUploadDir();
    }
    
    public function getWebPath(){
        
        return $this->getUploadDir().'/'.$this->id.'_'.$this->name;
    }

}